package com.example.huanhang.changzhouapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.example.huanhang.changzhouapp.R;
import com.example.huanhang.changzhouapp.activity.ListActivity;
import com.example.huanhang.changzhouapp.adapter.FeileiAdapter;
import com.example.huanhang.changzhouapp.base.BaseLazyFragment;
import com.example.huanhang.changzhouapp.bean.Feilei;
import com.example.huanhang.changzhouapp.util.ThreadUtil;
import com.gyf.barlibrary.ImmersionBar;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author : lei
 * @desc :底部标准文件内容
 * @date : 2018/9/13 0013  上午 8:35.
 * 个人博客站: http://www.bestlei.top
 */

public class FileFragment extends BaseLazyFragment {
    @BindView(R.id.tv_title)
    AppCompatTextView mTvTitle;
    @BindView(R.id.title)
    RelativeLayout mTitle;
    @BindView(R.id.main_progress)
    ProgressBar mMainProgress;
    @BindView(R.id.in_recycleview)
    RecyclerView mInRecycleview;
    FeileiAdapter flAdapter;
    List<Feilei> mList = new ArrayList<>();

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_file;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ImmersionBar.setTitleBar(getActivity(), mTitle);

    }
    @Override
    protected void initView() {
        super.initView();
        mMainProgress.setVisibility(View.VISIBLE);
        mInRecycleview.setLayoutManager(new LinearLayoutManager(getActivity()));
        flAdapter = new FeileiAdapter(R.layout.item_feilei, mList);
        flAdapter.setOnItemClickListener((adapter, view, position) -> {
            Feilei feilei = mList.get(position);
            Intent intent = new Intent(getActivity(), ListActivity.class);
            intent.putExtra("fl_name", feilei.getFlName());
            startActivity(intent);
        });
        mInRecycleview.setAdapter(flAdapter);
        ImmersionBar.with(this).statusBarColor(R.color.title).init();
    }

    @Override
    protected void initData() {
        super.initData();
        mTvTitle.setText("标准文件分类");
        getAdapterData();
    }
    private void getAdapterData() {
        if (mList.size() != 0) {
            mList.clear();
        }
        requestNet();

    }
    private void requestNet() {
        ThreadUtil.runOnBackThread(() -> {
            //已经排除非pdf文件
            final String sql = "SELECT DISTINCT lccb_fl FROM basic  WHERE  SUBSTRING(lccb_path, -3)='pdf' and lccb_fl not in ('程序文件','不确定度','质量表格','质量手册附件','质量手册') order by LENGTH(lccb_fl) ";
            try {
                ResultSet resultSet = ThreadUtil.result(sql);
                while (resultSet.next()) {
                    Feilei fl = new Feilei();
                    fl.setFlName(resultSet.getString("lccb_fl"));
                    mList.add(fl);
                }
                querySuccess();
                resultSet.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }

    private void querySuccess() {
        ThreadUtil.runOnUiThread(() -> {
            flAdapter.notifyDataSetChanged();
            mMainProgress.setVisibility(View.GONE);
        });
    }
}
